home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_1532.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  1.6 KB  |  12 lines

  1. Sometimes.
  2.  
  3. (First read the previous question on passing C++ objects to/from C functions.) You can safely access a C++ object's data from a C function if the C++ class:
  4.  * has no virtual functions (including inherited virtual fns)
  5.  * has all its data in the same access-level section (private/protected/public)
  6.  * has no fully-contained subobjects with virtual fns
  7.  
  8. If the C++ class has any base classes at all (or if any fully contained subobjects have base classes), accessing the data will *technically* be non-portable, since class layout under inheritance isn't imposed by the language.  However in practice, all C++ compilers do it the same way: the base class object appears first (in left-to-right order in the event of multiple inheritance), and subobjects follow.
  9.  
  10. Furthermore you can often (but less than always) assume a 'void*' appears in the object at the location of the first virtual function.  This is trickier, since the first virtual function is often in a different access specifier section than the data members.  Even the use of a single pointer is not required by the language (but this is the way 'everyone' does it).
  11.  
  12. If the class has any virtual base classes, it is more complicated and less portable.  One common implementation technique is for objects to contain an object of the virtual base class (V) last (regardless of where 'V' shows up as a virtual base class in the inheritance DAG), with the rest of the object's parts appearing in the normal order.  Every class that has V as a virtual base class actually has a *pointer* to the V part of the final object.